//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    orderListActive: 0,
    showOrder: -1,
    window: {},
    page: 1,
    page2: 1,
    nodata: 0,
    listData: [],
    listData2: [],
    type: 1,
    tabId: 1,
    year: 0,
    lastpage1: 0,
    lastpage2: 0,
    loading: 0,
    loaded1: 0,
    loaded2: 0,
  },

  canvas: function () {
    const ctx = wx.createCanvasContext('myCanvas');
    var window = wx.getSystemInfoSync();
    var windowWidth = window.windowWidth;

    ctx.beginPath()
    ctx.moveTo(0, 0)
    ctx.setFillStyle('#E6848B')
    ctx.bezierCurveTo(0, 80, windowWidth, 90, windowWidth, 0)
    ctx.setStrokeStyle('#E6848B')
    ctx.fill()
    ctx.stroke()
    ctx.draw();
  },

  onLoad: function (options) {
    var that = this;
    var d = new Date()
    that.setData({ type: options.type, year: d.getFullYear() });
    this.setData({ window: app.globalData.window })
    app.getUserInfo(function () {
      that.getList();
    });
    //this.canvas();
    var user_info = wx.getStorageSync('user_info');
    that.setData({ 'user_info': user_info });
    wx.setNavigationBarTitle({
      title: this.data.tabId == 0 ? this.data.year + "小计划" : '推荐'
    });
  },
  getrecommend() {
    var that = this;
    var page = that.data.page2;
    if (that.data.loaded2 == 1 || that.data.lastpage2 == 1) {
      return;
    }
    that.data.loaded2 = 1;
    //console.log(event_type);
    app.api("topic/list", {
      page: page,
      is_best: 1,
      type: 'todolist',
      pid2: 0,
      limit: 30,
      order: 'order_by',
    }, function (res) {
      //that.data.loaded2 = 1;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length > 0) {
        that.setData({ listData2: that.data.listData2.concat(res.data.list) });
      } else {
        that.data.lastpage2 = 1;
      }
      that.data.loading2 = 0;
      that.data.page2++;
    })
  },
  //api evens/list 活动列表 
  getList(type = 0) {

    if (type == 1) {
      this.setData({
        page: 1, lastPage: 0, page2: 1, lastPage2: 0, listData: [], listData2: [], loaded1: 0, loaded2: 0, loading: 0, lastpage1: 0
      });
      app.setPageData("user/timeline", { reload2: 0 });
    }

    if (this.data.loading == 1 || this.data.lastpage1 == 1) {
      return;
    }

    if (type == 2) {
      if (this.data.tabId == 0 && this.data.loaded1 == 1) {
        return false;
      }
      if (this.data.tabId == 1 && this.data.loaded2 == 1) {
        return false;
      }
    }
    if (this.data.tabId == 1) {
      this.getrecommend();
      return false;
    }

    this.data.loading = 1;
    // this.setData({ page:0,});
    var user_info = wx.getStorageSync('user_info');
    var that = this;
    var page = that.data.page;
    //console.log(event_type);
    if (that.data.tabId == 0) {
      that.data.loaded1 = 1;
    }
    if (that.data.tabId == 1) {
      that.data.loaded2 = 1;
    }
    app.api("topicjoin/get", {
      page: page,
      cate_id: this.data.showOrder,
      //type: 'todolist',
      // pid2:0,
      limit: 30,
      showType: 'my',
      order: 'SnsTopicJoin.addtime desc',
      user_id: user_info.user_id,
    }, function (res) {
      that.data.loading = 0;

      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      page++;
      if (res.data.list.length > 0) {
        that.setData({ listData: that.data.listData.concat(res.data.list) });
      } else {
        that.setData({ lastpage1: 1 });
      }
      if (that.data.listData.length == 0) {
        var nodata = 1;
      } else {
        var nodata = 0;
      }
      console.log(nodata);
      that.setData({ loading: 0, page: page, nodata: nodata });
      wx.stopPullDownRefresh();

    })
  },

  scrolltolower: function (e) {

    this.getList();
  },
  apply1: function () {
    wx.navigateTo({
      url: '/pages/index/issue'
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.getList(1);
  },
  bodyclick() {
    console.log('bodyclick');
    if (this.data.orderListActive == 1) {
      this.setData({ orderListActive: 0 });
    }
  },
  tapOrder(e) {
    this.data.orderListActive == 1 ? this.setData({ orderListActive: 0 }) : this.setData({ orderListActive: 1 });
    console.log('tapOrder');
  },
  indexTab: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({ tabId: id });
    this.getList(2);
    wx.setNavigationBarTitle({
      title: id == 0 ? this.data.year + "小计划" : '往年报告'
    });
  },
  swiperChange: function (e) {

    this.setData({ tabId: e.detail.current });
    this.getList(2);
    wx.setNavigationBarTitle({
      title: this.data.tabId == 0 ? this.data.year + "小计划" : '推荐'
    });
  },
  onShow: function () {
    //wx.hideTabBar();
    var showTab = app.getPageData("user/timeline", "showTab");
    var showOrder = app.getPageData("user/timeline", "showOrder");
    var reload = app.getPageData("user/timeline", 'reload2');
    if (showOrder && Number(showOrder) > -1) {
      this.data.showOrder = showOrder;
      app.setPageData("user/timeline", { showOrder: -1 });
      reload = 1;
    }
    if (showTab && Number(showTab) > -1) {
      this.setData({ tabId: 0 });
      app.setPageData("user/timeline", { showTab: -1 });
    }
    console.log(reload);
    if (reload == 1) {
      this.getList(1);
    }
  },
  changeOrder(e) {
    var key = e.currentTarget.dataset.key;
    var orderBy = e.currentTarget.dataset.order;
    //this.setData({ showOrder: key});
    this.setData({ orderListActive: 0, showOrder: key, orderBy: orderBy });
    if (this.data.tabId == 0) {
      this.getList(1);//加载数据 
    }
  },
  /**
* 页面上拉触底事件的处理函数
*/
  onReachBottom: function () {
    this.getList();
  },



})
